package fz.vrd.library.utils;

import android.content.Context;
import android.content.res.Resources;
import android.text.Html;
import android.text.Spanned;

import java.text.NumberFormat;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import fz.vrd.library.constant.Constant;


/**
  *  @说明: 字符串工具类
  *  @作者: VinRichard
  *  @时间: 2023/8/1 10:45
  *  @备注:
  */

public final class StringUtils  {

    public static boolean isNull(CharSequence... arr) {
        for (CharSequence cs : arr) {
            if (cs == null || cs.length() == 0 || "null".equals(cs.toString().toLowerCase())) {
                return true;
            }
        }
        return false;
    }

    public static boolean isObjNull(Object o) {

        return o == null ? true : (o instanceof CharSequence) ? isNull((CharSequence)o) : false;
    }

    public static String getDefaultMsg(String msg) {
        return getDefaultMsg(msg,"");
    }

    public static String getDefaultMsg(String msg, String defaultMsg) {
        return isNull(msg) ? defaultMsg : msg;
    }

    public static String trim(String str) {
        return str == null ? "" : str.trim();
    }

    public static double parseDouble(String num) {
        return parseDouble(num, 0);
    }

    public static double parseDouble(String num, double def) {
        double d;
        try {
            d = Double.parseDouble(num);
        } catch (Exception e) {
            d = def;
        }
        return d;
    }

    public static float parseFloat(String num) {
        return parseFloat(num, 0.0F);
    }

    public static float parseFloat(String num, float def) {
        float d;
        try {
            d = Float.parseFloat(num);
        } catch (Exception e) {
            d = def;
        }
        return d;
    }

    public static long parseLong(String num) {
        return parseLong(num,0);
    }

    public static long parseLong(String num,long def) {
        long d;
        try {
            d = Long.parseLong(num);
        } catch (Exception e) {
            d = def;
        }
        return d;
    }

    public static int parseInt(String num) {
        return parseInt(num,0);
    }

    public static int parseInt(String num,int def) {
        int d = 0;
        try {
            d = Integer.parseInt(num);
        } catch (Exception e) {
            d = def;
        }
        return d;
    }

    /**
     * 验证邮箱地址是否正确
     */
    public static boolean isEmail(String email) {
        return isEmail(email, Constant.Regex.EMAIL);
    }

    public static boolean isEmail(String email,String regex) {
        try {
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(email);
            return matcher.matches();
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 验证手机号码
     * 13,14,15,17,18,19 开头
     */
    public static boolean isMobile(String phone) {
        return isMobile(phone, Constant.Regex.MOBILE);
    }

    public static boolean isMobile(String phone, String regex) {
        try {
            return Pattern.compile(regex).matcher(phone).matches();
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 判断是否是纯数字
     */
    public static boolean isNum(String num) {

        return isNum(num,Constant.Regex.NUM);
    }

    public static boolean isNum(String num,String regex) {

        return isNull(num) ? false : Pattern.compile(regex).matcher(num).matches();
    }

    /**
     * 数字转化:如  1.0转为1   1.5转为1.5
     */
    public static String float2num(String msg) {

        if (!isNull(msg)) {
            if (msg.contains(".")) {
                String num[] = msg.replace(".", "@").split("@");
                if (num != null && num.length == 2) {
                    if (isNum(num[0]) && isNum(num[1])) {
                        msg = num[0] + (Integer.parseInt(num[1]) > 0 ? "." + num[1] : "");
                    }
                }
            }
        }
        return msg;
    }

    public static Class stringToClass(String cls) {
        try {
            return Class.forName(cls);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }



    /**
     * 计算百分比
     * @param num  分子
     * @param accout 分母
     * @param decimal 小数个数
     */
    public static String countPercent(int num, int accout, int decimal) {
        NumberFormat numberFormat = NumberFormat.getInstance();
        numberFormat.setMaximumFractionDigits(decimal);
        String result = numberFormat.format((float) num / (float) accout * 100);
        return result + "%";
    }

    public static String countPercent(int num, int accout) {
        return countPercent(num, accout, 2);
    }


    // 过滤特殊字符  清除掉所有特殊字符
    public static String stringFilter(String str) throws PatternSyntaxException {

        return stringFilter(str,Constant.Regex.STRING_TS);
    }

    public static String stringFilter(String str,String regex) throws PatternSyntaxException {

        return isNull(str) ? "" : Pattern.compile(regex).matcher(str).replaceAll("").trim();
    }

    /**
     * 首个数据是否是汉字
     */
    public static boolean msgFirstIsChinese(String string) {
        if (isNull(string)) {
            return false;
        }
        for (char c : string.toCharArray()) {
            return c >= 0x4E00 && c <= 0x9FA5;
        }
        return true;
    }

    /**
     * list的字符集合转换为字符串直接用逗号隔开(a,2,3,4,5,r,ty,sdf)
     */
    public static String getListToString(List<String> list) {
        String info = "";
        if (list != null) {
            for (String s : list) {
                info += isNull(info) ? s : "," + s;
            }
        }
        return info;
    }

    public static String hideTel(String tel) {
        if (isNum(tel) && tel.length() == 11) {
            tel = tel.substring(0, 3) + "****" + tel.substring(7);
        }
        return tel;
    }

    public static String hideIdCard(String idCard) {
        if (!isNull(idCard) && idCard.length() == 18) {
            idCard = idCard.substring(0, 6) + "********" + idCard.substring(14);
        }
        return idCard;
    }

   /**
    * 取得arrays.xml中的数据以数组的形式返回
    *
    * @param context  上下文
    * @param arrayres arrays.xml文件中的 string-name的名字
    * @return
    */
   public static String[] getStringFormArrays(Context context, int arrayres) {
       Resources res = context.getResources();
       return res.getStringArray(arrayres);
   }



   /**
    * 改变部分字体颜色
    * @param msg      原始字
    * @param colorMsg 需要改变的字的颜色
    * @param colorValue 颜色值: ff0000
    * @return
    */
   public static Spanned changMsgColor(String msg, String colorMsg,String colorValue) {
       if (isNull(msg) || isNull(colorMsg)) {
           return Html.fromHtml(msg);
       }
       int index = msg.indexOf(colorMsg);
       if (index == -1) {
           return Html.fromHtml(msg);
       }
       String beforeInfo = msg.substring(0, index);
       String endInfo = msg.substring(index + colorMsg.length());
       return Html.fromHtml(beforeInfo + "<font color= '#" + colorValue + "'>" + colorMsg + "</font>" + endInfo);
   }

}
